package com.yuexin.project.store.inbound.service;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yuexin.common.support.Convert;
import com.yuexin.project.store.inbound.domain.Inbound;
import com.yuexin.project.store.inbound.mapper.InboundMapper;
import com.yuexin.project.store.stock.domain.Stock;
import com.yuexin.project.store.stock.service.IStockService;

/**
 * 入库单 服务层实现
 * 
 * @author yuexin
 * @date 2018-11-29
 */
@Service
public class InboundServiceImpl implements IInboundService 
{
	@Autowired
	private InboundMapper inboundMapper;
	
	@Autowired
	private IStockService stockService;
	
	/**
     * 查询入库单信息
     * 
     * @param inboundId 入库单ID
     * @return 入库单信息
     */
    @Override
	public Inbound selectInboundById(Integer inboundId)
	{
	    return inboundMapper.selectInboundById(inboundId);
	}
	
	/**
     * 查询入库单列表
     * 
     * @param inbound 入库单信息
     * @return 入库单集合
     */
	@Override
	public List<Inbound> selectInboundList(Inbound inbound)
	{
	    return inboundMapper.selectInboundList(inbound);
	}
	
    /**
     * 新增入库单
     * 
     * @param inbound 入库单信息
     * @return 结果
     */
	@Override
	public int insertInbound(Inbound inbound)
	{
		//增加物品库存
		Integer goodsId = inbound.getGoodsId();
		Stock stock = stockService.selectStockById(goodsId);
		Integer amountOld = stock.getStockAmount();
		Integer amountNew = inbound.getInboundNumber();
		amountNew += amountOld;
		stock.setStockAmount(amountNew);
		stockService.updateStock(stock);
		
	    return inboundMapper.insertInbound(inbound);
	}
	
	/**
     * 修改入库单
     * 
     * @param inbound 入库单信息
     * @return 结果
     */
	@Override
	public int updateInbound(Inbound inbound)
	{
		//先在库存减去修改前的数量，再增加修改后的数量
		Integer inboundId = inbound.getInboundId();
		Inbound oldInBound = inboundMapper.selectInboundById(inboundId);
		Integer oldNumber = oldInBound.getInboundNumber();
		
	    Integer goodsId = inbound.getGoodsId();
		Stock stock = stockService.selectStockById(goodsId);
		Integer amount = stock.getStockAmount();
		Integer amountNew = inbound.getInboundNumber();
		amount = amount - oldNumber + amountNew;
		stock.setStockAmount(amount);
		stockService.updateStock(stock);
		
	    return inboundMapper.updateInbound(inbound);
	}

	/**
     * 删除入库单对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteInboundByIds(String ids)
	{
		String[] idArray = Convert.toStrArray(ids);
		for(int i = 0; i<idArray.length; i++) {
			Integer inboundId = Integer.valueOf(idArray[i]);
			Inbound inbound = inboundMapper.selectInboundById(inboundId);
			Integer number = inbound.getInboundNumber();
			
			Integer goodsId = inbound.getGoodsId();
			Stock stock = stockService.selectStockById(goodsId);
			Integer amount = stock.getStockAmount();
			amount = amount - number;
			stock.setStockAmount(amount);
			stockService.updateStock(stock);
			inboundMapper.deleteInboundById(inboundId);
		}
		
		return idArray.length;
	}
	
	@Override
	public int deleteInboundById(Integer inboundId)
	{
		Inbound inbound = inboundMapper.selectInboundById(inboundId);
		Integer number = inbound.getInboundNumber();
		
		Integer goodsId = inbound.getGoodsId();
		Stock stock = stockService.selectStockById(goodsId);
		Integer amount = stock.getStockAmount();
		amount = amount - number;
		stock.setStockAmount(amount);
		stockService.updateStock(stock);
		return inboundMapper.deleteInboundById(inboundId);
	}

}
